package cn.fintecher.pangolin.service.business.service;

import cn.fintecher.pangolin.common.enums.OtherFollowType;
import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.OtherRecord;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.service.business.model.request.AddWorkOrderRequest;
import cn.fintecher.pangolin.service.business.model.request.FindWorkOrderRequest;
import cn.fintecher.pangolin.service.business.model.request.ModifyWorkOrderRequest;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.OtherRecordElastic;
import cn.fintecher.pangolin.service.business.repository.jparepository.CaseInfoRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 14:41 2019/10/12
 */

@Service("workOrderService")
@Transactional(rollbackFor = Exception.class)
public class WorkOrderService {
    @Autowired
    private BaseService baseService;

    @Autowired
    private OtherRecordElastic otherRecordElastic;

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Autowired
    private ModelMapper modelMapper;

    /**
     * 添加工单
     */
    public void addWorkOrder(AddWorkOrderRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        CaseInfo caseInfo = caseInfoRepository.findById(request.getCaseId()).orElseThrow(() -> new BadRequestException(null, "caseInfo", "case.is.null"));
        OtherRecord otherRecord = new OtherRecord();
        otherRecord.setCertificateNo(request.getCertificateNo());
        otherRecord.setCaseId(request.getCaseId()); //案件ID
        otherRecord.setCaseNumber(caseInfo.getCaseNumber()); //案件编号
        otherRecord.setFindTime(request.getDate());
        otherRecord.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
        otherRecord.setRecord(request.getContent());
        otherRecord.setOtherFollowType(OtherFollowType.WORKORDER);
        otherRecord.setPrincipalId(caseInfo.getPrincipalId()); //委托方ID
        otherRecord.setPrincipalName(caseInfo.getPrincipalName()); //委托方名称
        otherRecord.process(userModel.getId(), userModel.getRealName(), ZWDateUtil.getNowDateTime());
        otherRecordElastic.save(otherRecord);
    }

    /**
     * 查询工单
     */
    public Page<OtherRecord> findAllWorkOrder(FindWorkOrderRequest request, Pageable pageable) {
        BoolQueryBuilder builder = request.getBuilder();
        SortBuilder sortBuilder = SortBuilders.fieldSort("date").unmappedType("date").order(SortOrder.DESC);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).withSort(sortBuilder).withPageable(pageable).build();
        return otherRecordElastic.search(searchQuery);
    }

    /**
     * 修改工单
     */
    public void modifyWorkOrder(ModifyWorkOrderRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        OtherRecord record = otherRecordElastic.findById(request.getId()).orElseThrow(() -> new BadRequestException(null, "workOrder", "workOrder.is.null"));
        record.setFindTime(request.getDate());
        record.setRecord(request.getContent());
        record.process(userModel.getId(), userModel.getRealName(), ZWDateUtil.getNowDateTime());
        otherRecordElastic.save(record);
    }

    /**
     * 删除工单
     */
    public void deleteWorkOrder(String id) {
        otherRecordElastic.deleteById(id);
    }
}
